//
//  BleManager.m
//  ZNHXManagement
//
//  Created by 四川中农 on 2018/10/30.
//  Copyright © 2018年 四川中农. All rights reserved.
//

#import "BleManager.h"
#import "printerlibs_caysnpos.h"
#import <UIKit/UIKit.h>

@implementation BleManager


static BleManager* _instance = nil;

+ (instancetype)share{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init] ;
    }) ;
    
    return _instance ;
}


-(wchar_t*)getWCharFromString:(NSString *)inStr{
    const char *cString = [inStr cStringUsingEncoding:NSUTF8StringEncoding];
    setlocale(LC_CTYPE, "UTF-8");
    wchar_t *outStr = NULL;
    size_t size = mbstowcs(NULL, cString, 0);
    outStr = new wchar_t[size + 1];
    if (outStr) {
        memset(outStr, 0, size * sizeof(wchar_t));
        size_t ret = mbstowcs(outStr, cString, size+1);
        if (ret == -1) {
            delete[] outStr;
            outStr = NULL;
        }
    }
    return outStr;
}


- (void)printerWithDictionary:(NSDictionary *)goodsDic andclosure:(dispatch_block_t)closure{
    if (self.pHandle != NULL) {
        
        void *h = self.pHandle;
        {
            CaysnPos_ResetPrinter(h);
            CaysnPos_SetMultiByteMode(h);
            CaysnPos_SetMultiByteEncoding(h, MultiByteModeEncoding_GBK);
        }
        
        {
            CaysnPos_SetAlignment(h, PosAlignment_Left);
            CaysnPos_SetTextBold(h, true);
            CaysnPos_SetTextScale(h, 1, 1);
            NSString *str = [NSString stringWithFormat:@"包裹号：%@\r\n",goodsDic[@"packageNumber"]];
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
        }
        
        {
            CaysnPos_ResetPrinter(h);
            CaysnPos_SetTextBold(h, false);
            CaysnPos_SetTextScale(h, 0, 0);
            NSString *str = [NSString stringWithFormat:@"订单号：%@\r\n",goodsDic[@"number"]];
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
        }
        
        {
            NSString *str = [NSString stringWithFormat:@"下单时间：%@\r\n",goodsDic[@"createTime"]];
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
        }
        
        {
            CaysnPos_SetTextBold(h, true);
            CaysnPos_SetTextScale(h, 1, 1);
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:@"包裹清单\n"]);
        }
        
        {
            CaysnPos_PrintHorizontalLineSpecifyThickness(h, 0, 400, 1);
        }
        
        CaysnPos_FeedLine(h, 1);
        CaysnPos_ResetPrinter(h);
        
        NSArray *arr = goodsDic[@"array"];
        for (NSInteger i = 0; i < arr.count; i++) {
            NSDictionary *dic = arr[i];
            NSString *name = [NSString stringWithFormat:@"%@",dic[@"goodsName"]];
            {
                CaysnPos_SetAlignment(h, PosAlignment_Left);
                CaysnPos_SetTextBold(h, false);
                CaysnPos_SetTextScale(h, 0, 0);
                CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:name]);
                
                NSString *specDesc = dic[@"specDesc"];
                NSString *num = dic[@"num"];
                NSString *spec = dic[@"spec"];
                NSString *str = [specDesc isEqualToString:@"(null)"] || specDesc == NULL ? [NSString stringWithFormat:@"%@%@\n",num,spec] : [NSString stringWithFormat:@"%@%@ x %@\n",num,spec,specDesc];
                CGRect strRect = [str boundingRectWithSize:CGSizeMake(1000,20)options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18]}context:nil];
                int margin = 400 - strRect.size.width - ([str rangeOfString:@"x"].location != NSNotFound ? 60 : 40);// - nameRect.size.width;
                CaysnPos_SetHorizontalAbsolutePrintPosition(h, margin);
                CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
            }
        }
        
        {
            CaysnPos_ResetPrinter(h);
            CaysnPos_SetTextBold(h, false);
            CaysnPos_SetTextScale(h, 0, 0);
            CaysnPos_FeedLine(h, 1);
            CaysnPos_PrintHorizontalLineSpecifyThickness(h, 0, 400, 1);
        }
        
        {
            CaysnPos_ResetPrinter(h);
            CaysnPos_SetAlignment(h, PosAlignment_Right);
            CaysnPos_SetTextBold(h, false);
            CaysnPos_SetTextScale(h, 0, 0);
            NSString *str = [NSString stringWithFormat:@"共计：%ld件商品",arr.count];
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
            CaysnPos_FeedLine(h, 2);
        }
        
        {
            CaysnPos_SetAlignment(h, PosAlignment_Left);
            CaysnPos_SetTextBold(h, true);
            CaysnPos_SetTextScale(h, 1, 1);
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:@"副联\n"]);
        }
        
        {
            CaysnPos_ResetPrinter(h);
            CaysnPos_SetTextBold(h, false);
            CaysnPos_SetTextScale(h, 0, 0);
            NSString *str = [NSString stringWithFormat:@"包裹号：%@\r\n",goodsDic[@"packageNumber"]];
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
        }
        
        {
            CaysnPos_SetTextBold(h, false);
            CaysnPos_SetTextScale(h, 0, 0);
            NSString *str = [NSString stringWithFormat:@"订单号：%@\r\n",goodsDic[@"number"]];
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:str]);
        }
        
        {
            CaysnPos_SetAlignment(h, PosAlignment_Left);
            CaysnPos_SetTextBold(h, true);
            CaysnPos_SetTextScale(h, 1, 1);
            CaysnPos_PrintTextInGBKW(h, [self getWCharFromString:@"客户签字:"]);
            CaysnPos_FeedLine(h, 4);
        }
        
        {
            
            int result = CaysnPos_Beep(h, 1, 500);
            if (!result){
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"写入失败，请稍后重试~" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                }];
                [alert addAction:confirm];
                [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
            }
        }
    }else{
        closure();
    }
    
}

@end
